package com.aznest.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.aznest.dto.CollectDTO;
import com.aznest.entity.*;
import com.aznest.enums.ResultCodeEnum;
import com.aznest.mapper.*;
import com.aznest.service.CollectService;
import com.aznest.exception.CustomException;
import com.aznest.vo.CollectVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 收藏信息表业务处理
 **/
@Service
@Slf4j
public class CollectServiceImpl implements CollectService {

    @Resource
    private CollectMapper collectMapper;
    @Resource
    private TypeMapper typeMapper;
    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 新增
     */
    public void add(CollectDTO collectDTO) {
        // 查询是否已经收藏了该酒店
        List<Collect> collects = collectMapper.selectByUserIdAndHotelId(collectDTO.getUserId(), collectDTO.getHotelId());
        // 如果已经收藏则抛出异常
        if (ObjectUtil.isNotEmpty(collects)) {
            throw new CustomException(ResultCodeEnum.COLLECTED_ALREADY_ERROR);
        }

        // 没有收藏则新增
        Collect collect = BeanUtil.copyProperties(collectDTO, Collect.class);
        collectMapper.insert(collect);
    }

    public List<CollectVO> selectByUserId(Integer userId) {
        List<Collect> collects = collectMapper.selectByUserId(userId);
        List<CollectVO> result = new ArrayList<>();
        for (Collect c : collects) {
            // 1. 找出该酒店的信息
            Hotel hotel = hotelMapper.selectById(c.getHotelId());
            if (ObjectUtil.isNotEmpty(hotel)) {
                CollectVO collectVO = BeanUtil.copyProperties(hotel, CollectVO.class);
                collectVO.setId(c.getId());
                collectVO.setUserId(c.getUserId());
                collectVO.setHotelId(c.getHotelId());
                // 计算最低价格
                // 找出酒店最低价格
                List<Type> typeList = typeMapper.selectByHotelId(collectVO.getHotelId());
                // 从typeList中找到最低价格
                Double minPrice = Double.MAX_VALUE;
                for (Type type : typeList) {
                    minPrice = Math.min(minPrice, type.getPrice());
                }
                // 设置酒店最低价格
                if (minPrice == Double.MAX_VALUE){
                    collectVO.setPrice(0d);
                }else{
                    collectVO.setPrice(minPrice);
                }

                // 计算评论数量
                List<Comment> comments = commentMapper.selectByHotelId(collectVO.getHotelId());
                collectVO.setCommentCount(comments.size());
                // 预订数量
                List<Orders> ordersList = ordersMapper.selectByHotelId(collectVO.getHotelId());
                collectVO.setBookCount(ordersList.size());

                // 添加到结果集
                result.add(collectVO);
            }
        }
        return result;
    }

    @Override
    public List<Collect> selectByUserIdAndHotelId(Integer userId, Integer hotelId) {
        List<Collect> collects = collectMapper.selectByUserIdAndHotelId(userId, hotelId);
        if(CollUtil.isEmpty(collects)){
            throw new CustomException(ResultCodeEnum.COLLECT_NOT_EXIST_ERROR);
        }
        return collects;
    }

    public void deleteByCollectId(Integer id) {
        collectMapper.deleteById(id);
    }
}
